package com.polaris.live.widget

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.polaris.live.adapter.MeGameAdapter
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.cacha.DisplayUtil
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.WidgetMeGameBinding
import com.polaris.live.resp.bean.MeGameBean
import com.polaris.live.resp.event.GameDotUpdateEvent
import com.polaris.live.resp.event.MainRouterEvent
import com.polaris.live.ui.game.GameUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * MeGameView 我的游戏界面View
 * @author Created by 天晴 on 2024/1/19 14:13
 * @since 1.0.0
 **/
class MeGameView(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) {

    private val mBinding = WidgetMeGameBinding.inflate(LayoutInflater.from(context), this)
    private val mAdapter by lazy { MeGameAdapter() }

    init {
        initView()
    }

    fun initView() {
        mBinding.imageGo.setOnSingleClickListener {
            EventBus.getDefault().post(
                MainRouterEvent(
                    DisplayUtil.getCurrentIndexByPrimaryId(DisplayUtil.GAME),
                    0
                )
            )
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            val data = mAdapter.data[position]
            GameUtils.showGameActivity(context, data.gameVo)
            //刷新红点
            mAdapter.data[position] = data.copy(showDot = false)
            mAdapter.notifyItemChanged(position)
        }

        mBinding.recyclerView.apply {
            addItemDecoration(object : ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State
                ) {
                    val position = parent.getChildAdapterPosition(view)
                    if (position != 0) {
                        if (context.resources.configuration.layoutDirection == LAYOUT_DIRECTION_RTL) {
                            outRect.right = 15.dp
                        } else {
                            outRect.left = 15.dp
                        }
                    }

                }
            })
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            adapter = mAdapter
        }
        setData()
    }

    fun setData() {
        visibility = if (GameUtils.getMeCacheGameList().isNullOrEmpty()) {
            GONE
        } else {
            VISIBLE
        }
        GameUtils.getMeCacheGameList()?.let { ggv ->
            mAdapter.data.clear()
            val dotList = GameUtils.getDotList()  //需要展示的游戏红点
            val dotEnterList = CacheUtil.getEnterDot()  //已经进入过的红点
            val newData = ggv
                .flatMap { gv -> gv.list }
                .map { da ->
                    MeGameBean(
                        da,
                        dotList.any { it == da.gameID } && dotEnterList.none { it == da.gameID }
                    )
                }
            mAdapter.data.addAll(newData)
        }
        mAdapter.notifyDataSetChanged()
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        EventBus.getDefault().register(this)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        EventBus.getDefault().unregister(this)
    }

    /**
     * 游戏红点更新
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onGameDotUpdateEvent(event: GameDotUpdateEvent) {
        setData()
    }
}